import requestData from '@/utils/request';
export default {
  namespaced: true,
  state: {
    swipers: [], // 轮播list
    booksInfo: [], // 图书列表
    saveBookInfoRes: [],
    bookDetail: {}, // 单本图书详情
  },
  actions: {
    async getSwiper ({commit}, payload) {
      const [res, err] = await requestData({
        url: '/books/getSwiper',
      });
      if (!err) {
        commit('updateBook', { swipers: res });
      }
    },
    async getBookInfo ({commit}, payload) {
      const { isbnCode, openId } = payload;
      const result = await requestData({
        method: 'GET', 
        url: '/books/saveBookInfo',
        data: {
          isbnCode,
          openId,
        }
      });
      commit('updateBook', { saveBookInfoRes: result });
    },
    async getBooks ({commit}, payload) {
      const [res, err] = await requestData({
        url: '/books/getList',
        data: payload,
      });
      if (!err) {
        commit('updateBook', { booksInfo: res });
      }
    },
    async getBookDetail ({ commit }, payload) {
      const [res, err] = await requestData({
        url: '/books/getDetail',
        data: payload,
      });
      if (!err) {
        commit('updateBook', { bookDetail: res });
      }
    },
  },
  mutations: {
    updateBook(state, payload) {
      Object.assign(state, payload);
    }
  }
}